package net.dgg.zz.production.suspend.service;

import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.StringUtils;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.datapermission.entity.DataAuthDTO;
import net.dgg.tmd.foundation.platform.datapermission.service.DataPermissionAssocService;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.treebook.entity.TreeBook;
import net.dgg.tmd.foundation.platform.treebook.service.TreeBookService;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.base.service.BaseService;
import net.dgg.zz.production.suspend.dao.SubOrderExtraDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 赵有文
 * @despriction 生产子订单（含超期状态）查询service
 * @create 2018/6/4 0004 下午 1:42
 **/
@Service
public class ZzSubOrderService extends BaseService {
    @Autowired
    private SubOrderExtraDao subOrderDao;
    @Autowired
    private DataPermissionAssocService dataPermissionAssocService;
    @Autowired
    protected DesUtil desUtil;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private TreeBookService treeBookService;

    private String[] transCols = new String[]{"originCode", "originType", "status", "abandonReason"};
    private String[] encryptCols = new String[]{"customerPhone"};

    public List<Map> queryMap(Map paras) {
        beforeQuery(paras);
        List<Map> resultList = subOrderDao.queryMap(paras);
        manageResult(resultList);
        return resultList;
    }

    public int count(Map paras) {
        beforeQuery(paras);
        return subOrderDao.count(paras);
    }

    public List<Map> queryOverOrder(Map paras) {
        beforeQuery(paras);
        List<Map> resultList = subOrderDao.queryOverOrder(paras);
        manageResult(resultList);
        return resultList;
    }

    public int queryOverOrderCount(Map paras) {
        beforeQuery(paras);
        return subOrderDao.queryOverOrderCount(paras);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Map queryMapById(String id) {
        Map para = new HashMap();
        para.put("id", id);
        List<Map> resultList = subOrderDao.queryMap(para);
        // Assert.isTrue(resultList != null && resultList.size() == 1, "未找到唯一的生产子订单记录！");
        manageResult(resultList);
        return resultList.get(0);
    }

    /**
     * 根据生产子订单编号查询
     *
     * @param no 生产子订单编号
     * @return
     */
    public Map queryMapByNo(String no) {
        Map para = new HashMap();
        para.put("fullScProductOrderNo", no);
        List<Map> resultList = subOrderDao.queryMap(para);
        Assert.isTrue(resultList != null && resultList.size() == 1, "未找到唯一的生产子订单记录！");
        manageResult(resultList);
        return resultList.get(0);
    }


    /**
     * @param paras
     */
    private void beforeQuery(Map paras) {
        String code = paras.get("dataAuthority").toString();
        //设置当前登录人
        UserEntity user = sessionManager.getCurrentUser();

        Assert.notNull(user, "检测到当前未登录。");
        DataAuthDTO dataAuthDTO = dataPermissionAssocService.selectAuthToConfigureRturnOrgIds(code);
        paras.put("userId", user.getId());
        paras.put("dataAuthDTO", dataAuthDTO);
        paras.put("userAlias", "t");
        paras.put("userColumn", "flow_user_id");
        paras.put("deptAlias", "t");
        paras.put("deptColumn", "flow_user_org_id");

        //查询条件号码加密
        if (!net.dgg.iboss.base.util.StringUtils.isEmptyObj(paras.get("customerPhone"))) {
            paras.put("customerPhone", desUtil.encrypt(paras.get("customerPhone").toString()));
        }
    }

    /**
     * 处理查询结果：
     * 1）字段翻译
     * 2) 客户号码脱敏
     *
     * @param resultList
     */
    private void manageResult(List<Map> resultList) {
        //翻译
        translateUtil.translateList(transCols, resultList);
        //解密
        resultList = desUtil.decryptListMap(encryptCols, resultList);
        //脱敏
        CustomerNumberEncryptionUtil.encryptionList(encryptCols, resultList);
    }

    public List<Map> getCheckCloseAuditOrder(List<Long> ids) {
        return subOrderDao.getCheckCloseAuditOrder(ids);
    }
}
